<!-- * WanlShop - App全局模板配置
 * @author 深圳前海万联科技有限公司 <wanlshop@i36k.com> 
 * @link http://www.wanlshop.com
 * 
 * @本程序受法律保护，著作权©软著登记号2020SR0255711 < 非FastAdmin购买授权，未经版权所有权人书面许可，不能用于商业用途！>
 * 
 * @获取许可
 * 后台下载：https://www.fastadmin.net/store/wanlshop.html
 * 帮助文档：https://doc.fastadmin.net/wanlshop
 * 
 * @stress 本程序仅用作FastAdmin付费插件（WanlShop B2B2C商城）API使用，非FastAdmin购买授权，未经版权所有权人书面许可，不能自行用于商业用途
 * @creationtime  2019年9月10日12:52:20
 * @lasttime 2020年6月9日09:26:07
 * @version 1.0.0
 -->
<script>
	import Vue from 'vue';
	import qqmapwx from "@/static/js/qqmap-wx-jssdk.min.js"
	import config from "@/common/config/config.js"
	export default {
		onLaunch(options) {
			

			
			
			
			// 检测客户端更新
			this.$store.dispatch('update/update');
			// 仅首次打开一次性加载数据
			this.$store.dispatch('common/init');
			// 同步购物车 本地 & 网络
			this.$store.dispatch('cart/get');
			// 同步 动态数据
			this.$store.dispatch('statistics/get');

			this.getAddress();

			//plus.navigator.hideSystemNavigation();
		},
		onShow() {
			//this.initConfig();
			//this.getAddress();
			// 启动或重启即时通讯
			console.log(123);
			this.$store.dispatch('chat/connectSocket');
		},
		onHide() {
			// plus.navigator.hideSystemNavigation();
			// 断开即时通讯
			this.$store.dispatch('chat/close');
		},
		methods: {
			initConfig() {
				uni.request({
					url: '/index/config',
					success: (res) => {
						const msg = res.msg;
						uni.setStorageSync('isShow', msg);
					}
				});
			},

			getAddress() {

				let that = this;

				//------------------------------------------

				if (config.test) {
					let add = {
						latitude: 32.1524,
						longitude: 111.372,
						address: {
							city: "襄阳市",
							citycode: "0710",
							country: "中国",
							district: "谷城县",
							province: "湖北省",
							towncode: "420625500000",
							township: "薤山林场",
							latitude: 32.1524,
							longitude: 111.372,
							street: "汉和路",
							address_reference: {
								street: {
									title: "仙人渡"
								}
							}

						}
					};

					this.$store.commit('common/setAddress', add);
				} else {
					let qqmapsdk = new qqmapwx({
						key: '7LHBZ-JGG6W-72RR6-YDOJY-LNRCS-FVB55' //后端给的key
					});

					uni.getLocation({
						type: 'gcj02',
						geocode: true,
						isHighAccuracy: true,
						altitude: true,
						success: res => {
							const latitude = res.latitude;
							const longitude = res.longitude;

							qqmapsdk.reverseGeocoder({
								location: {
									latitude,
									longitude
								},
								success: function(res) {

									let address = res.result.address_component;
									address.township = res.result.address_reference.town.title;

									try {
										address.neighborhoodname = res.result.address_reference
											.street.title ? res.result.address_reference.street
											.title : "";
										address.street = res.result.address_reference.street
											.title ? res.result.address_reference.street.title :
											"";
									} catch (err) {
										address.neighborhoodname = "";
										address.street = ""
									}


									address.neighborhood = {
										name: address.neighborhoodname
									};
									const addressData = {
										address: address,
										latitude: latitude,
										longitude: longitude,
										township: address.township,
										neighborhood: {
											name: address.neighborhoodname
										}
									}
									address.longitude = longitude;
									address.latitude = latitude;

									uni.setStorageSync('addressData', JSON.stringify(
										address)); /*全局缓存相关的数据*/
									that.$store.commit('common/setAddress', addressData);

									console.log(that.$store.state.common.addressData);

								},
								fail: function(error) {
									console.error(error);
								},
								complete: function(res) {
									console.log(res);
								}
							})





							// 以高德地图API为例
							// 					  const amapKey = '817dcdcf5e5a3c606f6f13d76d563e8a';
							// 					  const url = `https://restapi.amap.com/v3/geocode/regeo?key=${amapKey}&location=${longitude},${latitude}`;
							// 					  uni.request({
							// 						url: url,
							// 						success: (response) => {

							// 							console.log(response);
							// 							let address = response.data.regeocode.addressComponent;

							// 						 /*
							// 						 App address =>{"city":"襄阳市","province":"湖北省",
							// 						 "adcode":"420606","district":"樊城区",
							// 						 "towncode":"420606102000",
							// 						 "streetNumber":{"number":"2号",
							// 						 "location":"112.131502,32.076881","direction":"东北",
							// 						 "distance":"53.0314","street":"追日路"},"country":"中国",
							// 						 "township":"团山镇","businessAreas":[[]],
							// 						 "building":{"name":[],"type":[]},"neighborhood":{"name":[],"type":[]},"citycode":"0710"}
							// 						 */
							// 						  const addressData = {
							// 							address:address,
							// 							latitude:latitude,
							// 							longitude:longitude
							// 						  }						  
							// 						  address.longitude = longitude;
							// 						  address.latitude = latitude;

							// 						  uni.setStorageSync('addressData', JSON.stringify(address));/*全局缓存相关的数据*/
							// 						  this.$store.commit('common/setAddress', addressData);
							// 						}
							// 					  });



						}
					});


				}

			}
		}
	};
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	@import 'static/style/colorui.css';
	@import 'static/style/wanlshop.css';
	@import 'static/style/iconfont.css';
	/* #endif */
	
	@import "@/uni.scss";
</style>